//
//  SLOperFriendTableViewCell.m
//  StarLight
//
//  Created by Duke.wu on 16/6/3.
//  Copyright © 2016年 Duke.Wu. All rights reserved.
//

#import "SLOperFriendTableViewCell.h"
#import "SLShipOwnerVipLevelView.h"

@interface SLOperFriendTableViewCell ()
@property (weak, nonatomic) IBOutlet UIImageView *friendIcon;
@property (weak, nonatomic) IBOutlet UIButton *operBtn;
@property (weak, nonatomic) IBOutlet UILabel *friendNameLb;
@property (weak, nonatomic) IBOutlet UILabel *phoneContactLb;
@property (weak, nonatomic) IBOutlet UILabel *friendTypeLb;
@property (weak, nonatomic) IBOutlet UILabel *VIPLB;
@property (strong, nonatomic) SLShipOwnerVipLevelView *vipLevelView;
@property (nonatomic, strong) SLOperbuddyModel *model;
@end


@implementation SLOperFriendTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
  
  self.selectionStyle = UITableViewCellSelectionStyleNone;
  [self.contentView addSubview:self.vipLevelView];
  [self.vipLevelView mas_makeConstraints:^(MASConstraintMaker *make) {
    make.centerY.equalTo(self.VIPLB);
    make.left.equalTo(self.VIPLB.mas_right).offset(4);
    make.size.mas_equalTo(CGSizeMake(20, 20));
  }];
  
  [self.operBtn addTarget:self action:@selector(DidTapOperBtn) forControlEvents:UIControlEventTouchUpInside];
}


- (void)configOperFriendCellWithModel:(SLOperbuddyModel *)model{
  self.model = model;
  self.vipLevelView.hidden = NO;
  self.VIPLB.hidden = NO;
  self.phoneContactLb.hidden = NO;
  [self.friendIcon sd_setImageWithURL:[NSURL URLWithString:model.headPhoto] placeholderImage:[UIImage imageNamed:@"head"]];
  self.friendNameLb.text = model.name;
  self.friendTypeLb.text = model.type == 0 ? @"船主" : @"货主";
  [self changeStatusWithState:model.state];
  [self changeWithRole:model.role];
  
  if(model.contacts){
    [self.friendTypeLb mas_updateConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.friendIcon).offset(-12);
    }];
    self.phoneContactLb.text = [NSString stringWithFormat:@"手机联系人：%@",model.contacts];
  }else{
    self.phoneContactLb.hidden = YES;
    [self.friendTypeLb mas_updateConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.friendIcon);
    }];
  }
  
  if (model.bz) {
    [self.friendTypeLb mas_updateConstraints:^(MASConstraintMaker *make) {
      make.centerY.equalTo(self.friendIcon).offset(-12);
    }];
    self.phoneContactLb.hidden = NO;
    self.phoneContactLb.text = [NSString stringWithFormat:@"备注：%@",model.bz];
  }
  
}

- (void)changeWithRole:(NSInteger)role{
  if (role < 1) {
    self.vipLevelView.hidden = YES;
    self.VIPLB.hidden = YES;
  }else{
    self.vipLevelView.hidden = NO;
    self.VIPLB.hidden = NO;
    [self.vipLevelView configWithLevel:[NSString stringWithFormat:@"%ld",(long)role]];
  }
}

- (void)DidTapOperBtn{
  if (_delegate && [_delegate respondsToSelector:@selector(didTapedoperFriendBtnWith:)]) {
    [_delegate didTapedoperFriendBtnWith:self.model];
  }
}


- (void)changeStatusWithState:(NSInteger)state{
  self.operBtn.enabled = YES;
  switch (state) {
    case -1:{
      [self.operBtn setTitle:@"  取消屏蔽  " forState:UIControlStateNormal];
      self.operBtn.backgroundColor = [UIColor grayColor];
    }
      break;
    case 0:{
      [self.operBtn setTitle:@"  添加  " forState:UIControlStateNormal];
      self.operBtn.backgroundColor = SLYellowColor;
    }
      break;
    case 1:{
      [self.operBtn setTitle:@"  已添加  " forState:UIControlStateNormal];
      self.operBtn.backgroundColor = [UIColor grayColor];
      self.operBtn.enabled = NO;
    }
      break;
    case 2:{
      [self.operBtn setTitle:@"  接受  " forState:UIControlStateNormal];
      self.operBtn.backgroundColor = SLYellowColor;
    }
     break;
      
    default:
      break;
  }

}

- (SLShipOwnerVipLevelView *)vipLevelView{
  if (!_vipLevelView) {
    _vipLevelView = [SLShipOwnerVipLevelView new];
  }
  return _vipLevelView;
}


- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
